/*
Open Source Initiative OSI - The MIT License:Licensing

The MIT License

Copyright (c) <year> <copyright holders>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Jesse Warden
jesterxl@jessewarden.com
jessewarden.com
twitter.com/jesterxl
*/

/**
*	The main log window GUI.  This guy can die, but the log messages will remain.  The convention
 * is that you're only supposed to create 1 of these, but I don't enforce it; Buyer beware. 
*/

package
{
	import com.jxl.debug.Colors;
	import com.jxl.debug.DebugMaxItemRenderer;
	import com.jxl.debug.Message;
	import com.jxl.debug.MessageType;
	import com.jxl.debug.debug_mizznax;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.filters.DropShadowFilter;
	import flash.net.SharedObject;
	import flash.system.System;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Button;
	import mx.controls.CheckBox;
	import mx.controls.List;
	import mx.controls.TabBar;
	import mx.core.ClassFactory;
	import mx.core.UIComponent;
	import mx.events.CloseEvent;
	import mx.events.CollectionEvent;
	import mx.events.ItemClickEvent;
	import mx.managers.PopUpManager;
	
	use namespace debug_mizznax;
	
	[Event(name="close", type="mx.events.CloseEvent")]
	public class Debug extends UIComponent
	{
		
		public static const USER_ALL:String				= "All";
		public static const USER_DEVELOPER_B:String 	= "DeveloperB";
		
		public static var DEBUG_DISABLED:Boolean		= false;
		
		private static const ALLOWED_USERS:Array 		= [USER_DEVELOPER_B, USER_ALL];
		
		public function Debug():void
		{
			super();
			
			inst 		= this;
			
			this.setStyle("color", 0x333333);
			
			var debugging:Boolean = true;
			// use the below if you wish to compiler configuration, like:
			// -define+=CONFIG::debugging,true
			//var debugging:Boolean = CONFIG::debugging;
			if(debugging)
			{
				DEBUG_DISABLED = false;
			}
			else
			{
				DEBUG_DISABLED = true;
			}
			
			if(DEBUG_DISABLED) return;
			
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
			
			var backlogSource:Array = backlog.source.concat();
			backlogSource.sortOn("date", Array.NUMERIC);
			messages	= new ArrayCollection(backlogSource);
			messages.addEventListener(CollectionEvent.COLLECTION_CHANGE, onMessagesChanged);
			backlog.removeAll();
			backlog 	= null;
		}
		
		public static function logHeader(user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace(HEADER_TEXT);
			
			if(inst)
			{
				inst.logHeader(user);
			}
			else
			{
				backlog.addItem(new Message(MessageType.LOG, HEADER_TEXT, user));
			}
		}
		
		public static function debugHeader(user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace(HEADER_TEXT);
			
			if(inst)
			{
				inst.debugHeader(user);
			}
			else
			{
				backlog.addItem(new Message(MessageType.DEBUG, HEADER_TEXT, user));
			}
		}
		
		public static function infoHeader(user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace(HEADER_TEXT);
			
			if(inst)
			{
				inst.infoHeader(user);
			}
			else
			{
				backlog.addItem(new Message(MessageType.INFO, HEADER_TEXT, user));
			}
		}
		
		public static function warnHeader(user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace(HEADER_TEXT);
			
			if(inst)
			{
				inst.warnHeader(user);
			}
			else
			{
				backlog.addItem(new Message(MessageType.WARN, HEADER_TEXT, user));
			}
		}
		
		public static function errorHeader(user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace(HEADER_TEXT);
			
			if(inst)
			{
				inst.errorHeader(user);
			}
			else
			{
				backlog.addItem(new Message(MessageType.ERROR, HEADER_TEXT, user));
			}
		}
		
		public static function fatalHeader(user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace(HEADER_TEXT);
			
			if(inst)
			{
				inst.fatalHeader(user);
			}
			else
			{
				backlog.addItem(new Message(MessageType.FATAL, HEADER_TEXT, user));
			}
		}
		
		public static function log(o:*, user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace("Log:::" + o);
			
			if(inst)
			{
				inst.log(o, user);
			}
			else
			{
				backlog.addItem(new Message(MessageType.LOG, o, user));
			}
		}
		
		public static function debug(o:*, user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace("Debug:::" + o);
			
			if(inst)
			{
				inst.debug(o, user);
			}
			else
			{
				backlog.addItem(new Message(MessageType.DEBUG, o, user));
			}
		}
		
		public static function info(o:*, user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace("Info:::" + o);
			
			if(inst)
			{
				inst.info(o, user);
			}
			else
			{
				backlog.addItem(new Message(MessageType.INFO, o, user));
			}
		}
		
		public static function warn(o:*, user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace("Warn:::" + o);
			
			if(inst)
			{
				inst.warn(o, user);
			}
			else
			{
				backlog.addItem(new Message(MessageType.WARN, o, user));
			}
		}
		
		public static function error(o:*, user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace("Error:::" + o);
			
			if(inst)
			{
				inst.error(o, user);
			}
			else
			{
				backlog.addItem(new Message(MessageType.ERROR, o, user));
			}
		}
		
		public static function fatal(o:*, user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace("Fatal:::" + o);
			
			if(inst)
			{
				inst.fatal(o, user);
			}
			else
			{
				backlog.addItem(new Message(MessageType.FATAL, o, user));
			}
		}
		
		public function logHeader(user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace(HEADER_TEXT);
			messages.addItem(new Message(MessageType.LOG, HEADER_TEXT, user));
		}
		
		public function log(o:*, user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace(o);
			messages.addItem(new Message(MessageType.LOG, o, user));
		}
		
		public function debugHeader(user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace(HEADER_TEXT);
			messages.addItem(new Message(MessageType.DEBUG, HEADER_TEXT, user));
		}
		
		public function debug(o:*, user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace(o);
			messages.addItem(new Message(MessageType.DEBUG, o, user));
		}
		
		public function infoHeader(user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace(HEADER_TEXT);
			messages.addItem(new Message(MessageType.INFO, HEADER_TEXT, user));
		}
		
		public function info(o:*, user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace(o);
			messages.addItem(new Message(MessageType.INFO, o, user));
		}
		
		public function warnHeader(user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace(HEADER_TEXT);
			messages.addItem(new Message(MessageType.WARN, HEADER_TEXT, user));
		}
		
		public function warn(o:*, user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace(o);
			messages.addItem(new Message(MessageType.WARN, o, user));
		}
		
		public function errorHeader(user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace(HEADER_TEXT);
			messages.addItem(new Message(MessageType.ERROR, HEADER_TEXT, user));
		}
		
		public function error(o:*, user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace(o);
			messages.addItem(new Message(MessageType.ERROR, o, user));
		}
		
		public function fatalHeader(user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace(HEADER_TEXT);
			messages.addItem(new Message(MessageType.FATAL, HEADER_TEXT, user));
		}
		
		public function fatal(o:*, user:String=USER_ALL):void
		{
			if(DEBUG_DISABLED) return;
			
			if(ALLOWED_USERS.indexOf(user) < 0) return;
			
			trace(o);
			messages.addItem(new Message(MessageType.FATAL, o, user));
		}
		
		public function get autoScroll():Boolean { return _autoScroll; }
		public function set autoScroll(value:Boolean):void
		{
			_autoScroll = value;
			autoScrollDirty = true;
			invalidateProperties();
		}
		
		protected override function measure():void
		{
			measuredMinWidth 		= 120;
	        measuredMinHeight 		= 120;
	        measuredWidth 			= 540;
	        measuredHeight 			= 400;
		}
		
		protected override function createChildren():void
		{
			super.createChildren();
			
			background = new Sprite();
			addChild(background);
			background.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			background.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			background.buttonMode = true;
			
			tabs = new TabBar();
			tabs.includeInLayout = false;
			addChild(tabs);
			tabs.dataProvider = [
									NAMES[MessageType.LOG],
								 	NAMES[MessageType.DEBUG], 
								 	NAMES[MessageType.INFO], 
								 	NAMES[MessageType.WARN], 
								 	NAMES[MessageType.ERROR], 
								 	NAMES[MessageType.FATAL]
								 ];
			tabs.addEventListener(ItemClickEvent.ITEM_CLICK, onItemClick);
			tabs.height = 22;
			tabs.setStyle("tabWidth", 70);
			
			list = new List();
			list.includeInLayout = false;
			addChild(list);
			list.dataProvider = messages;
			list.variableRowHeight = true;
			list.wordWrap = true;
			//list.dataTipField = "content";
			//list.showDataTips = true;
			list.labelField = "content";
			list.allowMultipleSelection = true;
			list.itemRenderer = new ClassFactory(DebugMaxItemRenderer);
			list.setStyle("borderStyle", "solid");
			list.setStyle("borderThickness", 4);
			
			clearButton = new Button();
			clearButton.includeInLayout = false;
			addChild(clearButton);
			clearButton.addEventListener(MouseEvent.CLICK, onClear);
			clearButton.height = 22;
			clearButton.label = "Clear";
			
			copyButton = new Button();
			copyButton.includeInLayout = false;
			addChild(copyButton);
			copyButton.addEventListener(MouseEvent.CLICK, onCopy);
			copyButton.height = 22;
			copyButton.width = 120;
			copyButton.label = "Copy to Clipboard";
			
			scrollCheckBox = new CheckBox();
			scrollCheckBox.includeInLayout = false;
			addChild(scrollCheckBox);
			scrollCheckBox.label = "auto-scroll";
			scrollCheckBox.selected = autoScroll;
			scrollCheckBox.addEventListener(Event.CHANGE, onToggleAutoScroll);
			scrollCheckBox.setActualSize(100, 22);
			scrollCheckBox.styleName = "checkBoxStyle";
			
			closeButton = new Button();
			closeButton.includeInLayout = false;
			addChild(closeButton);
			closeButton.addEventListener(MouseEvent.CLICK, onClose);
			closeButton.setActualSize(16, 16);
			
			var drop:DropShadowFilter = new DropShadowFilter(3, 45, 0x333333, .7, 4, 4, 1, 1, false, false, false);
			filters = [drop];
			
			callLater(scrollIt);
		}
		
		protected override function commitProperties():void
		{
			super.commitProperties();
			
			if(autoScrollDirty)
			{
				autoScrollDirty = false;
				scrollCheckBox.selected = _autoScroll;
			}
		}
		
		protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			background.graphics.clear();
			background.graphics.beginFill(0x333333, .8);
			background.graphics.drawRoundRect(0, 0, width, height, 6, 6);
			background.graphics.endFill();
			
			closeButton.move(width - (closeButton.width + MARGIN), 2);
			tabs.move(MARGIN, MARGIN);
			tabs.setActualSize(width - (MARGIN * 2), tabs.height);
			scrollCheckBox.move(width - (scrollCheckBox.width + MARGIN), height - (scrollCheckBox.height + MARGIN));
			clearButton.move(MARGIN, height - (clearButton.height + MARGIN));
			clearButton.setActualSize(width - scrollCheckBox.width - copyButton.width - (MARGIN * 5), clearButton.height);
			copyButton.move(clearButton.x + clearButton.width + MARGIN, clearButton.y);
			list.move(tabs.x, tabs.y + tabs.height);
			list.setActualSize(width - (MARGIN * 2), height - clearButton.height - (MARGIN * 2) - list.y);
		}
		
		
		
		private function onMessagesChanged(event:CollectionEvent):void
		{
			scrollIt();
			destroyScrollTimer();
			if(scrollTimer == null)
			{
				scrollTimer = new Timer(500);
				scrollTimer.addEventListener(TimerEvent.TIMER, onTick, false, 0, true);
				scrollTimer.start();
			}
		}
		
		private function destroyScrollTimer():void
		{
			if(scrollTimer)
			{
				scrollTimer.stop();
				scrollTimer.removeEventListener(TimerEvent.TIMER, onTick);
				scrollTimer = null;
			}
		}
		
		private function onTick(event:TimerEvent):void
		{
			scrollIt();
			if(_autoScroll == false) 	destroyScrollTimer();
			if(list == null) 			destroyScrollTimer();
			if(list.verticalScrollPosition == list.maxVerticalScrollPosition) destroyScrollTimer();
		}
		
		private function scrollIt():void
		{
			if(_autoScroll)
			{
				if(list)
				{
					list.verticalScrollPosition = list.maxVerticalScrollPosition;
				}
			}	
		}
		
		private function onClear(event:MouseEvent):void
		{
			messages.removeAll();
		}
		
		private function onCopy(event:MouseEvent):void
		{
			var str:String = "";
			var len:int = messages.length;
			for(var i:uint = 0; i < len; i++)
			{
				var message:Message = messages[i] as Message;
				str += NAMES[message.type] + "  " + message.content + "\n";
			}
			try
			{
				System.setClipboard(str);
			}
			catch(err:Error)
			{
				Debug.error(Debug, "onCopy, Failed to to copy to clipboard.");
				Debug.error(Debug, err.toString()); 
			}
		}
		
		private function onToggleAutoScroll(event:Event):void
		{
			_autoScroll = scrollCheckBox.selected;
		}
		
		private function onClose(event:MouseEvent):void
		{
			inst = null;
			if(messages)
			{
				var messagesSource:Array = messages.source.concat();
				backlog = new ArrayCollection(messagesSource);
				messages.removeAll();
				messages = null;
			}
			PopUpManager.removePopUp(this);
			dispatchEvent(new CloseEvent(CloseEvent.CLOSE));
		}
		
		private function onItemClick(event:ItemClickEvent):void
		{
			switch(event.item)
			{
				case NAMES[MessageType.LOG]:
					filterMessages(MessageType.LOG);
					list.setStyle("borderColor", Colors.LOG);
					break;
				
				case NAMES[MessageType.DEBUG]:
					filterMessages(MessageType.DEBUG);
					list.setStyle("borderColor", Colors.DEBUG);
					break;
				
				case NAMES[MessageType.INFO]:
					filterMessages(MessageType.INFO);
					list.setStyle("borderColor", Colors.INFO);
					break;
				
				case NAMES[MessageType.WARN]:
					filterMessages(MessageType.WARN);
					list.setStyle("borderColor", Colors.WARN);
					break;
				
				case NAMES[MessageType.ERROR]:
					filterMessages(MessageType.ERROR);
					list.setStyle("borderColor", Colors.ERROR);
					break;
				
				case NAMES[MessageType.FATAL]:
					filterMessages(MessageType.FATAL);
					list.setStyle("borderColor", Colors.FATAL);
					break;
			}
		}
		
		private function filterMessages(type:uint):void
		{
			switch(type)
			{
				case MessageType.LOG:
					messages.filterFunction = null;
					break;
					
				case MessageType.DEBUG:
					messages.filterFunction = function(message:Message):Boolean
					{
						if(message.type >= MessageType.DEBUG)
						{
							return true;
						}
						return false;
					};
					break;
				
				case MessageType.INFO:
					messages.filterFunction = function(message:Message):Boolean
					{
						if(message.type >= MessageType.INFO)
						{
							return true;
						}
						return false;
					};
					break;
				
				case MessageType.WARN:
					messages.filterFunction = function(message:Message):Boolean
					{
						if(message.type >= MessageType.WARN)
						{
							return true;
						}
						return false;
					};
					break;
				
				case MessageType.ERROR:
					messages.filterFunction = function(message:Message):Boolean
					{
						if(message.type >= MessageType.ERROR)
						{
							return true;
						}
						return false;
					};
					break;
				
				case MessageType.FATAL:
					messages.filterFunction = function(message:Message):Boolean
					{
						if(message.type >= MessageType.FATAL)
						{
							return true;
						}
						return false;
					};
					break;
				
			}
			messages.refresh();
		}
		
		// Dragging Functionality
		private function onAddedToStage(event:Event):void
		{
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			try
			{
				var so:SharedObject = SharedObject.getLocal("DebugMax_position");
				if(so != null)
				{
					if(so.data.pos != null)
					{
						if(so.data.pos.x != undefined && so.data.pos.y != undefined)
						{
							move(so.data.pos.x, so.data.pos.y);
						}
					}
				}
			}
			catch(err:Error)
			{
			}
		}
		
		private function onRemovedFromStage(event:Event):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		
		private function onMouseDown(event:MouseEvent):void
		{
			if(dragging == false)
			{
				dragging = true;
				this.startDrag();
				this.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			}
		}
		
		private function onMouseMove(event:MouseEvent):void
		{
			event.updateAfterEvent();
		}
		
		private function onMouseUp(event:MouseEvent):void
		{
			if(dragging)
			{
				dragging = false;
				this.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
				this.stopDrag();
				event.updateAfterEvent();
				
				var so:SharedObject = SharedObject.getLocal("DebugMax_position");
				so.data.pos = {x: x, y: y};
				var r:String = so.flush();
			}
		}
		
		private static const MARGIN:Number = 4;
		debug_mizznax static var NAMES:Dictionary;
		private static var constrctured:Boolean = classConstruct();
		
		private static function classConstruct():Boolean
		{
			NAMES = new Dictionary();
			NAMES[MessageType.LOG] 			= "Log";
			NAMES[MessageType.DEBUG] 		= "Debug";
			NAMES[MessageType.INFO] 		= "Info";
			NAMES[MessageType.WARN] 		= "Warn";
			NAMES[MessageType.ERROR] 		= "Error";
			NAMES[MessageType.FATAL] 		= "Fatal";
			return true;
		}
		
		private static var inst:Debug;
		private static var backlog:ArrayCollection				= new ArrayCollection();
		private static const HEADER_TEXT:String					= "---------------------";
				
		private var messages:ArrayCollection;
		private var _autoScroll:Boolean							= true;
		private var autoScrollDirty:Boolean						= false;
		private var dragging:Boolean							= false;
		private var scrollTimer:Timer;
		
		private var tabs:TabBar;
		private var list:List;
		private var clearButton:Button;
		private var scrollCheckBox:CheckBox;
		private var closeButton:Button;
		private var background:Sprite;
		private var copyButton:Button;
		
	}
}